const Controllers = require('../controllers/Controller.js').controller;
const controller = require("../controllers/ControllerVip").controller
const formatTime_2 = require("../../utils/util").formatTime_2
const app = getApp();
Page({
  data: {
    globalStype:'',
    couponList: [],
    myList:[],
    type:'',
    otherList:[],
    typeId:0,
    shopId:'',
    pageNumber:1,
    lastPage:false,
    backbool:true,
    src1:"../../assets/my/img/quan_bg.png",
    src2:"../../assets/my/img/quan_unsel.png",
    src3:"../../assets/my/img/quan_gq.png",
    src4:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210416/832568918034874368.png",
  },

  //选择导航
  selectType(e){
    let typeId = e.currentTarget.dataset['id'];
    this.setData({
      couponList:[],
      pageNumber:1,
      lastPage:false
    })
    if(typeId==1){
      this.getMyCouponPage(typeId)
    }else if(typeId==0){
      this.getMyCoupon(0)
    }else{
      this.getMyCoupon(1)
    }
    this.setData({
      typeId
    })
  },

  //获取店铺详情
  getShopDetail(){
    Controllers.shopDetails({shopId:wx.getStorageSync('shopId')}).then(res=>{
      this.setData({
        shopDetail:res.data
      })
    })
  },

  //获取我的优惠券
  getMyCoupon(id){
    let value={}
    value.shopId=this.data.shopId
    value.state=id
    value.couponType=0
    controller.couponList(value).then(res=>{
        let my=res.data.my;
        let other=res.data.other;

        for(var i=0;i<my.length;i++){
          if(my[i].timeState!=0){
            if(my[i].effectiveStartTime){
              my[i].effectiveStartTime=my[i].effectiveStartTime.split(" ")[0];
            }
            if(my[i].effectiveEndTime){
              my[i].effectiveEndTime=my[i].effectiveEndTime.split(" ")[0];
            }
            
          }
        }
        for(var i=0;i<other.length;i++){
          if(other[i].timeState!=0){
          if(other[i].effectiveStartTime){
            other[i].effectiveStartTime=other[i].effectiveStartTime.split(" ")[0];
          }
          if(other[i].effectiveEndTime){
            other[i].effectiveEndTime=other[i].effectiveEndTime.split(" ")[0];
          }
          }
        }
        this.setData({
          myList:my,
          otherList:other
        })

      this.setData({
        couponList:res.data
      })
    })
  },

  //跳转详情
  gopage(e){
    let url = e.currentTarget.dataset['url'];
    wx.navigateTo({
      url: url
    })
  },

  //跳转优惠券详情页面
  jumpToDetail(e){
    let cstate=e.currentTarget.dataset.cstate;
    let id=e.currentTarget.dataset.id;
    let state=e.currentTarget.dataset.state
    let pushid=e.currentTarget.dataset.pushid
    let couponaccountid=e.currentTarget.dataset.couponaccountid
    let isvip=e.currentTarget.dataset.isvip
    let url=""
    if(isvip==1){
      url='/pages/Mall/couponDetailBuy?id='+id+"&couponAccountId="+couponaccountid+"&out=1"+"&shopId="+this.data.shopId
    }else{
      url='/pages/Mall/Coupondetail?fromUrl=1&id='+id+"&type="+state+"&couponPushId="+pushid+'&isuserdetail=1'+'&couponAccountId='+couponaccountid
    }
    if(cstate===0){
      wx.navigateTo({
        url: url,
      })
    }else{
      wx.showToast({
        icon:"none",
        title: '优惠券已过期/已使用',
      })
    }
  },

  //获取优惠券列表
  getMyCouponPage(state) {
    if (!wx.getStorageSync('usertoken')) {
        app.GetLogin()
        return
    }
    if(state==1){
      Controllers.activityPage({ shopId: this.data.shopId,pageNumber:this.data.pageNumber,pageSize:10}).then(res => {
        if (res.code == 1) {
          let couponList = this.data.couponList
          res.data.list.forEach(element => {
            if(element.timeState == 1 && element.effectiveStartTime && element.effectiveEndTime){
              element.effectiveStartTime = formatTime_2(new Date(element.effectiveStartTime.replace(/-/g, '/')))
              element.effectiveEndTime = formatTime_2(new Date(element.effectiveEndTime.replace(/-/g, '/')))
            }
          });
          couponList = couponList.concat(res.data.list)

          this.setData({
            couponList,
            lastPage:res.data.lastPage
          })
          console.log(this.data.couponList)
        }
      })
    }else{
      Controllers.activityPage({ shopId: this.data.shopId,pageNumber:this.data.pageNumber,pageSize:10, state: state }).then(res => {
        if (res.code == 1) {
          let couponList = this.data.couponList
          res.data.list.forEach(element => {
            if(element.timeState == 1 && element.effectiveStartTime && element.effectiveEndTime){
              element.effectiveStartTime = formatTime_2(new Date(element.effectiveStartTime.replace(/-/g, '/')))
              element.effectiveEndTime = formatTime_2(new Date(element.effectiveEndTime.replace(/-/g, '/')))
            }
          });
          couponList = couponList.concat(res.data.list)
          this.setData({
            couponList,
            lastPage:res.data.lastPage
          })
          console.log(this.data.couponList)
        }
      })
    }
  },

  onLoad(option) {
    console.log(option);
    this.setData({globalStype:app.globalData.globalStype})
    if(getCurrentPages().length<=2){
      this.setData({
        backbool:false
      })
    }else{
      this.setData({
        backbool:true
      })
    }
    if (option.shopId) {
      this.setData({
        shopId: option.shopId,
      })
      console.log(this.data.backbool);
      app.globalData.shopId = option.shopId
      wx.setStorageSync('shopId', option.shopId)
    }
   
    
    if(option.invitationAccountId){
      this.setData({
        invitationAccountId:option.invitationAccountId
      })
    }
    if(option.num){
      this.setData({
        num:option.num
      })
      this.readNum()
    }
    if(option.type){
      this.setData({
        type:option.type
      })
    }
    this.getShopDetail()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.lastPage&&this.data.typeId==1){
      let pageNumber = this.data.pageNumber+1
      this.setData({
        pageNumber
      })
      this.getMyCouponPage(this.data.typeId)
    }
  },

  onShow() {
    this.setData({
      couponList: [],
      pageNumber:1,
      lastPage:false
    })
    if (app.globalData.shopId) {
      this.setData({
        shopId: app.globalData.shopId
      })
    }
    if (wx.getStorageSync('usertoken')) {
        //加载首页数据
        this.getMyCoupon(0)
        this.myInformation()
        if(this.data.invitationAccountId){
          this.invitationPerson()
        }
    }else{
      // app.GetLogin()
      //无usertoken，等待login登录回调
    app.usertokenReadyCallback = res => {
      if(res.data.sessionId){
        this.getMyCoupon(0)
        this.myInformation()
        if(this.data.invitationAccountId){
          this.invitationPerson()
        }
      }
    }
    }

    
  },

  // onUnload: function () {
  //   if(this.data.type){
  //     wx.redirectTo({
  //       url: '/pages/my/my-index/my-index'
  //     })
  //   }
  // },

  myInformation(){
    //获取数据
    Controllers.myInformation({shopId:this.data.shopId}).then(res => {
      this.setData({
        myInformationdata:res.data.accountRelation
      })
      app.globalData.myshopinfo=res.data.accountRelation
    })
  },
  onShareAppMessage: function (e) {
    let obj = {
      title: this.data.shopDetailsdata.shopName,
      path:'/pages/Mall/Coupon?shopId=' + this.data.shopId+"&invitationAccountId="+this.data.myInformationdata.accountId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  onShareTimeline: function (e) {
    let obj = {
      title: '',
      path:'/pages/Mall/Coupon?shopId=' + this.data.shopId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  backindex(e){
    console.log(e)
    if (wx.getStorageSync('usertoken')) {
        wx.navigateTo({
          url:'/pages/my/my-index/my-index'
        })
    }else{
      app.GetLogin();
    }
  },

  invitationPerson(){
    let value={}
    value.invitationAccountId=this.data.invitationAccountId
    value.shopId=wx.getStorageSync('shopId')
    Controllers.invitationPerson(value).then(res=>{
        
    })
  },


  //修改已读
  readNum(){
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.name="shopCoupon"
    value.num=this.data.num
    Controllers.readNum(value).then(res=>{

    })
  }
})